import parseInt from 'lodash/parseInt';

export const tryParseInt = (value, defaultValue = 0) => {
    const resultValue = parseInt(value);

    if (isNaN(resultValue)) {
        return defaultValue;
    }
    return resultValue;
};

export function addClass(elm, className) {
    if (!className) return;

    const els = Array.isArray(elm) ? elm : [elm];

    els.forEach(el => {
        if (el.classList) {
            el.classList.add(className.split(' '));
        } else {
            el.className += ` ${className}`;
        }
    });
}

export function removeClass(elm, className) {
    if (!className) return;

    const els = Array.isArray(elm) ? elm : [elm];

    els.forEach(el => {
        if (el.classList) {
            el.classList.remove(className.split(' '));
        } else {
            el.className = el.className.replace(
                new RegExp(
                    `(^|\\b)${className.split(' ').join('|')}(\\b|$)`,
                    'gi'
                ),
                ' '
            );
        }
    });
}
